<?php
/****************************************************************************

	COPYRIGHT(C) WWW.MAXSI.DK, MAXSI SOFTWARE, SORTIE 2009
	ALL RIGHTS RESERVED

	Maxsi Distribution ContentServer/ Branch
	Scripts running a Maxsi Distribution Compatible Content Server.	
	
	downloadblocks.php
	Donloads a series of blocks from a local arcive.

****************************************************************************/

	// Todo: Make an application/x-MaxsiDistributionContentServerTerminal protocol.
	
	// See if the block is locked.
	
	// Todo: See if the correct variables are being passed.
	// Todo: Load variables from GET, POST and COOKIE!
	
	if ( get_cfg_var('safe_mode') == 0 )
	{
		set_time_limit(0);
	}
	//error_reporting(0);
		
	$RequestLine		=	md_RequestParam('RequestLine');
	$ContentArchive		=	md_RequestParam('contentarchive');
	$TorrentId			=	md_RequestParam('torrentid');
	
	if (!isset($RequestLine))
	{
		mdc_SendErrorDie('Failure! No RequestLine');
	}	
	if (!isset($ContentArchive))
	{
		mdc_SendErrorDie('Failure! No contentarchive');
	}
	if (!isset($TorrentId))
	{
		mdc_SendErrorDie('Failure! No torrentid');
	}
	
	
	$slot	=	md_sql('SELECT locked FROM '.md_GetDatabaseName('contentarchive').' WHERE name = "'.md_SlashGlobalVar($ContentArchive).'"');
	
	if ( $md_sql_total[$slot] )
	{
		if ($md_sql_row[$slot]['locked'])
		{
			mdc_SendErrorDie('This content archive is locked because it is being updated. Please wait!');
		}		
	}
	else
	{
		// This content server is not supported. Fail!
		mdc_SendErrorDie('Failure! No such content archive!');
		
		// Todo: Possible hacking possibillity: People can download .php sources via this code.
	}
	
	$BlockList		=	explode(" ",stripslashes($RequestLine));
	
	$NumBlocks		=	count($BlockList);
	
	//header('Content-Type: application/x-MaxsiTorrentHTTPReply');
	//header('Transfer-Encoding: chunked');
	header("Content-Transfer-Encoding: binary"); 
	header('Content-Type: text/plain');
		
	$counter	=	0;
	$CHUNKSIZE	=	512*1024;
	
	$ContentArchiveId		=	md_ContentArchiveNameToContentArchiveId(md_SlashGlobalVar($ContentArchive));
	
	do
	{
		$file_name			=	md_ProductNameToFileName($ContentArchive).'/'.md_ProductNameToFileName($ContentArchive).'_Block_'.$BlockList[$counter].'.MaxsiDistribution';

		if (file_exists($file_name))
		{
			$file_size			=	filesize($file_name);
			$left				=	$file_size;
			echo $TorrentId. "\n";
			echo $BlockList[$counter] . "\n";
			echo $file_size . "\n";
			
			while ($left > 0)
			{
				if ($left > $CHUNKSIZE)
				{
					echo file_get_contents($file_name,true,NULL,$file_size-$left,$CHUNKSIZE );
					md_creditbandwidth($ContentArchiveId,$CHUNKSIZE);
					$left-=$CHUNKSIZE;
				}
				else
				{
					echo file_get_contents($file_name,true,NULL,$file_size-$left,$left );
					md_creditbandwidth($ContentArchiveId,$left);
					$left=0;
				}
			}				
		}
		else
		{
			// Todo!
		}
	
		$counter++;
	}
	while ($counter < $NumBlocks)
?>